/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.feiyue.basic.reader.util.CustomToast;
import com.feiyue.basic.server.db.BookCollectDBHandler;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Book;
import com.feiyue.helper.BookListAdapter;
import com.umeng.analytics.MobclickAgent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BookListActivity extends Activity implements
		AbsListView.OnScrollListener, View.OnTouchListener,
		GestureDetector.OnGestureListener {
	private List<Book> booklist;
	private int categoryid;
	private String categoryname;
	private Context context;
	private ImageView goback;
	private Handler handler;
	private boolean hasmore = true;
	private int lastbottom;
	private ListView list;
	private ArrayList<HashMap<String, Object>> listItem;
	private BookListAdapter listItemAdapter;
	private LinearLayout list_linear;
	public List<Book> localbooklist;
	private GestureDetector mGestureDetector;
	private MessageReceiver messageReceiver;
	private int minVelocity = 150;
	private LinearLayout morebooks;
	private Myrunnable myrunnable;
	private boolean thread2getnewbookisrunning = false;
	private int verticalMinDistance = 200;

	public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
		if (!(this.mGestureDetector.onTouchEvent(paramMotionEvent)))
			return super.dispatchTouchEvent(paramMotionEvent);
		return true;
	}

	public void load() {
		this.booklist.addAll(this.localbooklist);
		int i = this.localbooklist.size();
		for (int j = 0;; ++j) {
			if (j >= i) {
				this.listItemAdapter.notifyDataSetChanged();
				this.morebooks.setVisibility(8);
				this.list_linear.setVisibility(0);
				return;
			}
			HashMap localHashMap = new HashMap();
			localHashMap.put("ItemTile",
					((Book) this.localbooklist.get(j)).getName());
			localHashMap.put("ItemAuthor",
					"作者:" + ((Book) this.localbooklist.get(j)).getAuthor());
			localHashMap.put("ItemIntroduction", "介绍:"
					+ ((Book) this.localbooklist.get(j)).getIntroduction());
			this.listItem.add(localHashMap);
		}
	}

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(2130903042);
		this.mGestureDetector = new GestureDetector(this);
		LinearLayout localLinearLayout = (LinearLayout) findViewById(2131099684);
		localLinearLayout.setOnTouchListener(this);
		localLinearLayout.setLongClickable(true);
		this.context = getApplicationContext();
		this.categoryname = getIntent().getExtras().getString("categoryname");
		this.categoryid = getIntent().getExtras().getInt("categoryid");
		this.morebooks = ((LinearLayout) findViewById(2131099690));
		this.morebooks.setVisibility(8);
		this.list_linear = ((LinearLayout) findViewById(2131099688));
		((TextView) findViewById(2131099687)).setText(this.categoryname);
		this.list = ((ListView) findViewById(2131099689));
		this.list.setOnScrollListener(this);
		this.booklist = new ArrayList();
		this.localbooklist = new ArrayList();
		this.listItem = new ArrayList();
		this.listItemAdapter = new BookListAdapter(this, this.listItem,
				2130903043, new String[] { "ItemTile", "ItemAuthor",
						"ItemIntroduction" }, new int[] { 2131099695,
						2131099696, 2131099697 }, this.booklist);
		this.list.setAdapter(this.listItemAdapter);
		this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> paramAdapterView,
					View paramView, int paramInt, long paramLong) {
				BookListActivity.this.setTitle("点击第" + paramInt + "个项目");
				String str1 = ((Book) BookListActivity.this.booklist
						.get(paramInt)).getName();
				int i = ((Book) BookListActivity.this.booklist.get(paramInt))
						.getId();
				int j = ((Book) BookListActivity.this.booklist.get(paramInt))
						.getCoverid();
				String str2 = ((Book) BookListActivity.this.booklist
						.get(paramInt)).getCoverAddr();
				int k = ((Book) BookListActivity.this.booklist.get(paramInt))
						.getdlstate();
				String str3 = ((Book) BookListActivity.this.booklist
						.get(paramInt)).getAuthor();
				String str4 = ((Book) BookListActivity.this.booklist
						.get(paramInt)).getIntroduction();
				String str5 = ((Book) BookListActivity.this.booklist
						.get(paramInt)).getIssuestate();
				Intent localIntent = new Intent(BookListActivity.this
						.getApplicationContext(), BookBriefActivity.class);
				localIntent.putExtra("categoryname",
						BookListActivity.this.categoryname);
				localIntent.putExtra("categoryid",
						BookListActivity.this.categoryid);
				localIntent.putExtra("bookname", str1);
				localIntent.putExtra("bookid", i);
				localIntent.putExtra("author", str3);
				localIntent.putExtra("introduction", str4);
				localIntent.putExtra("issuestate", str5);
				localIntent.putExtra("coverid", j);
				localIntent.putExtra("coverAddr", str2);
				localIntent.putExtra("dlstate", k);
				BookListActivity.this.startActivity(localIntent);
			}
		});
		this.myrunnable = new Myrunnable();
		this.handler = new Handler();
		this.morebooks.setVisibility(0);
		this.list_linear.setVisibility(8);
		this.thread2getnewbookisrunning = true;
		new MyThread().start();
		this.messageReceiver = new MessageReceiver();
		IntentFilter localIntentFilter = new IntentFilter(
				"MESSAGERECEIVER_BOOKID");
		registerReceiver(this.messageReceiver, localIntentFilter);
		this.goback = ((ImageView) findViewById(2131099686));
		this.goback.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BookListActivity.this.finish();
				BookListActivity.this.overridePendingTransition(2130968586,
						2130968589);
			}
		});
	}

	protected void onDestroy() {
		unregisterReceiver(this.messageReceiver);
		super.onDestroy();
	}

	public boolean onDown(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onFling(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		float f = Math.abs(paramMotionEvent1.getX() - paramMotionEvent2.getX())
				/ Math.abs(paramMotionEvent1.getY() - paramMotionEvent2.getY());
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() > 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968587, 2130968588);
			return true;
		}
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() < 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968586, 2130968589);
			return true;
		}
		return false;
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if (paramInt == 4) {
			finish();
			overridePendingTransition(2130968586, 2130968589);
			return true;
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}

	public void onLongPress(MotionEvent paramMotionEvent) {
	}

	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onScroll(AbsListView abslistview, int i, int j, int k)
    {
        if (lastbottom != i + j) {
        	lastbottom = i + j;
            if (i + j == k && k != 0)
            {
                morebooks.setVisibility(0);
                if (!thread2getnewbookisrunning)
                {
                    thread2getnewbookisrunning = true;
                    (new MyThread()).start();
                }
            } else
            {
                morebooks.setVisibility(8);
            }
        }
    }

	public boolean onScroll(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		return false;
	}

	public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
	}

	public void onShowPress(MotionEvent paramMotionEvent) {
	}

	public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
		return this.mGestureDetector.onTouchEvent(paramMotionEvent);
	}

	public class MessageReceiver extends BroadcastReceiver {
		public void onReceive(Context paramContext, Intent paramIntent) {
			try {
				BookCollectDBHandler localBookCollectDBHandler = new BookCollectDBHandler(
						paramContext);
				BookListActivity.this.listItemAdapter
						.setCollectbooklist(localBookCollectDBHandler
								.getBookList(paramContext));
				localBookCollectDBHandler.destroy();
				return;
			} catch (Exception localException) {
			}
		}
	}

	   private class MyThread extends Thread
	    {

	        public void run()
	        {
	            if (hasmore)
	            {
	                localbooklist = NovelInfo.getBookList(context, NovelInfo.getUrl2Booklist(categoryid, booklist.size(), 20), categoryid, booklist.size(), 20);
	                if (localbooklist != null && localbooklist.size() != 0)
	                {
	                    myrunnable.setisload(true);
	                } else
	                {
	                    hasmore = false;
	                    myrunnable.setisvisible(true);
	                }
	                handler.post(myrunnable);
	            } else
	            {
	                myrunnable.setisvisible(true);
	                handler.post(myrunnable);
	            }
	            super.run();
	        }

	    }

	private class Myrunnable implements Runnable {
		public boolean isload = false;
		public boolean isvisible = false;

		private void reset() {
			this.isload = false;
			this.isvisible = false;
		}

		public void run() {
			if (this.isload)
				BookListActivity.this.load();
			if (this.isvisible) {
				new CustomToast(BookListActivity.this.context).show(
						"已显示全部内容，请等待更新", 300);
				BookListActivity.this.morebooks.setVisibility(8);
			}
			reset();
			BookListActivity.this.thread2getnewbookisrunning = false;
		}

		public void setisload(boolean paramBoolean) {
			this.isload = paramBoolean;
		}

		public void setisvisible(boolean paramBoolean) {
			this.isvisible = paramBoolean;
		}
	}
}